﻿Imports System.Xml
Imports Microsoft.VisualBasic.ControlChars

Public Class EditXML
    Public Shared sPdelim As String = "£"

    Public Sub Edit(ByVal node2edit As String, ByVal nodeValue As String)
        Dim thatNode As XmlNode
        thatNode = frmMain.xmlDoc.SelectSingleNode(node2edit)
        thatNode.InnerText = nodeValue
        frmMain.xmlDoc.Save(frmMain.strFileToSave)
        Edited()
    End Sub
    Public Sub Add(ByVal node2add As String, ByVal node2addafter As String, ByVal nodeName As String, ByVal nodevalue As String)
        Dim thatNode As XmlNode
        Dim insertAfterNode As XmlNode
        Dim node2insert As XmlElement
        thatNode = frmMain.xmlDoc.SelectSingleNode(node2add)
        insertAfterNode = frmMain.xmlDoc.SelectSingleNode(node2add & node2addafter)
        node2insert = frmMain.xmlDoc.CreateElement(nodeName)
        node2insert.InnerXml = nodevalue
        thatNode.InsertAfter(node2insert, insertAfterNode)
        frmMain.xmlDoc.Save(frmMain.strFileToSave)
        Edited()
    End Sub
    Public Sub Remove(ByVal node2removeFrom As String, ByVal nodes2remove As String)
        Dim thatNode As XmlNode
        thatNode = frmMain.xmlDoc.SelectSingleNode(node2removeFrom)
        For i As Integer = thatNode.ChildNodes.Count To 1 Step -1
            If thatNode.ChildNodes(i - 1).Name = nodes2remove Then thatNode.RemoveChild(thatNode.ChildNodes(i - 1))
        Next
        frmMain.xmlDoc.Save(frmMain.strFileToSave)
        Edited()
    End Sub
    Public Function Extract(ByVal pathName As String, ByVal rootName As String) As String
        Dim thatNode As String = ""
        Dim nodeList As XmlNodeList = frmMain.xmlDoc.SelectNodes(pathName)
        For i As Integer = nodeList.Count() - 1 To 0 Step -1
            thatNode = thatNode & nodeList(i).InnerXml()
        Next
        Dim strStylesheet1 As String = "<?xml-stylesheet href="
        Dim strStylesheet2 As String = " type='text/xsl'?>"
        Dim strCurHref As String = "'file:///" & Application.StartupPath & "\NBII_classic.xsl'"
        Dim strMetaHeader As String = "" ' "<?xml version='1.0'?>" & CrLf
        'strMetaHeader = strMetaHeader & strStylesheet1 & strCurHref & strStylesheet2 & CrLf & _
        strMetaHeader = strMetaHeader & "<" & rootName & ">" & thatNode & "</" & rootName & ">"
        Return strMetaHeader
    End Function
    Public Sub Edited()
        frmMain.isFileModified = True
    End Sub
    Public Sub NotEdited()
        frmMain.isFileModified = False
    End Sub
    Public Function createCiteInfo(ByVal citation As CiteInfo, ByVal boolLWC As Boolean, ByVal citation2 As CiteInfo) As String
        Dim newNode As String = ""
        newNode = newNode & "<citeinfo><origin>" & citation.Author & "</origin>"
        newNode = newNode & "<pubdate>" & citation.PubDate & "</pubdate>"
        newNode = newNode & "<pubtime>" & citation.PubTime & "</pubtime>"
        newNode = newNode & "<title>" & citation.Title & "</title><edition>" & citation.Edition & "</edition>"
        newNode = newNode & "<geoform>" & citation.Geoform & "</geoform>"
        newNode = newNode & "<serinfo><sername>" & citation.SerialName & "</sername><issue>" & citation.Issue & "</issue></serinfo>"
        newNode = newNode & "<pubinfo><pubplace>" & citation.PubPlace & "</pubplace><publish>" & citation.Publisher & "</publish></pubinfo>"
        newNode = newNode & "<othercit>" & citation.OtherCit & "</othercit><onlink>" & citation.URLink & "</onlink>"
        If boolLWC Then
            'Dim citation2 As CiteInfo = item.ToolCiteLWC()
            newNode = newNode & "<lworkcit><citeinfo>"
            newNode = newNode & "<origin>" & citation2.Author & "</origin>"
            newNode = newNode & "<pubdate>" & citation2.PubDate & "</pubdate>"
            newNode = newNode & "<pubtime>" & citation2.PubTime & "</pubtime>"
            newNode = newNode & "<title>" & citation2.Title & "</title><edition>" & citation2.Edition & "</edition>"
            newNode = newNode & "<geoform>" & citation2.Geoform & "</geoform>"
            newNode = newNode & "<serinfo><sername>" & citation2.SerialName & "</sername><issue>" & citation2.Issue & "</issue></serinfo>"
            newNode = newNode & "<pubinfo><pubplace>" & citation2.PubPlace & "</pubplace><publish>" & citation2.Publisher & "</publish></pubinfo>"
            newNode = newNode & "<othercit>" & citation2.OtherCit & "</othercit><onlink>" & citation2.URLink & "</onlink>"
            newNode = newNode & "</citeinfo></lworkcit>"
        End If
        newNode = newNode & "</citeinfo>"
        Return newNode
    End Function
    Public Function createTimeInfo(ByVal timeValues As Collection, ByVal boolSingle As Boolean, ByVal boolRange As Boolean) As String
        Dim newNode As String = ""
        If boolSingle And boolRange Then
            newNode = newNode & "<timeinfo><mdattim>"
            For j As Integer = timeValues.Count To 1 Step -1
                Dim value As SngDateTime = timeValues(j)
                newNode = newNode & "<sngdate><caldate>" & value.SngDate & "</caldate><time>" & value.TimeOfDay & "</time></sngdate>"
            Next
            newNode = newNode & "</mdattim></timeinfo>"
        ElseIf boolSingle Then
            Dim value As SngDateTime = timeValues(1)
            newNode = newNode & "<timeinfo><sngdate><caldate>" & value.SngDate & "</caldate><time>" & value.TimeOfDay & "</time></sngdate></timeinfo>"
        ElseIf boolRange Then
            Dim begValue As SngDateTime = timeValues(1)
            Dim endValue As SngDateTime = timeValues(2)
            newNode = newNode & "<timeinfo><rngdates><begdate>" & begValue.SngDate & "</begdate><begtime>" & begValue.TimeOfDay & "</begtime><enddate>" & endValue.SngDate & "</enddate><endtime>" & endValue.TimeOfDay & "</endtime></rngdates></timeinfo>"
        End If
        Return newNode
    End Function
    Public Function createGeoTimeInfo(ByVal timeValues As Collection, ByVal boolSingle As Boolean, ByVal boolRange As Boolean) As String
        Dim newNode As String = ""
        If boolSingle And boolRange Then ''Geological Multiple
            newNode = newNode & "<timeinfo><mdattim>"
            For j As Integer = timeValues.Count To 1 Step -1
                Dim value As GeolAge = timeValues(j)
                newNode = newNode & "<sngdate><geolage><geolscal>" & value.TimeScale & "</geolscal><geolest>" & value.Estim & "</geolest><geolun>" & value.Uncert & "</geolun><geolexpl>" & value.Explain & "</geolexpl><geolcit>"
                'Geological Cite Info
                For k As Integer = value.GCiteCount To 1 Step -1
                    newNode = newNode & createCiteInfo(value.GCiteFetch(k), value.GCbFetchLWC(k), value.GCiteFetchLWC(k)) 'Last 2 was j(?)
                Next
                newNode = newNode & "</geolcit></geolage></sngdate>"
            Next
            newNode = newNode & "</mdattim></timeinfo>"
        ElseIf boolSingle Then 'Geological Single
            Dim value As GeolAge = timeValues(1)
            newNode = newNode & "<timeinfo><sngdate><geolage><geolscal>" & value.TimeScale & "</geolscal><geolest>" & value.Estim & "</geolest><geolun>" & value.Uncert & "</geolun><geolexpl>" & value.Explain & "</geolexpl><geolcit>"
            'Geological Cite Info
            For j As Integer = value.GCiteCount To 1 Step -1
                newNode = newNode & createCiteInfo(value.GCiteFetch(j), value.GCbFetchLWC(j), value.GCiteFetchLWC(j))
            Next
            newNode = newNode & "</geolcit></geolage></sngdate></timeinfo>"
        ElseIf boolRange Then 'Geological Range
            Dim begValue As GeolAge = timeValues(1)
            Dim endValue As GeolAge = timeValues(2)

            newNode = newNode & "<timeinfo><rngdates><beggeol><geolage><geolscal>" & begValue.TimeScale & "</geolscal><geolest>" & begValue.Estim & "</geolest><geolun>" & begValue.Uncert & "</geolun><geolexpl>" & begValue.Explain & "</geolexpl><geolcit>"
            'Geological Cite Info for beginning
            For j As Integer = begValue.GCiteCount To 1 Step -1
                newNode = newNode & createCiteInfo(begValue.GCiteFetch(j), begValue.GCbFetchLWC(j), begValue.GCiteFetchLWC(j))
            Next
            newNode = newNode & "</geolcit></geolage></beggeol>"

            newNode = newNode & "<endgeol><geolage><geolscal>" & endValue.TimeScale & "</geolscal><geolest>" & endValue.Estim & "</geolest><geolun>" & endValue.Uncert & "</geolun><geolexpl>" & endValue.Explain & "</geolexpl><geolcit>"
            'Geological Cite Infor for ending
            For j As Integer = endValue.GCiteCount To 1 Step -1
                newNode = newNode & createCiteInfo(endValue.GCiteFetch(j), endValue.GCbFetchLWC(j), endValue.GCiteFetchLWC(j))
            Next
            newNode = newNode & "</geolcit></geolage></endgeol>"

            newNode = newNode & "</rngdates></timeinfo>"
        End If
        Return newNode
    End Function
    Public Function createContactInfo(ByVal objDataContact As ContactInfo) As String
        Dim newNode As String = ""
        newNode = newNode & "<cntinfo>"
        If objDataContact.PrimaryPerson() Then newNode = newNode & "<cntperp>" Else newNode = newNode & "<cntorgp>"
        newNode = newNode & "<cntper>" & objDataContact.Person & "</cntper><cntorg>" & objDataContact.Organization & "</cntorg>"
        If objDataContact.PrimaryPerson() Then newNode = newNode & "</cntperp>" Else newNode = newNode & "</cntorgp>"
        newNode = newNode & "<cntpos>" & objDataContact.Position & "</cntpos>"
        For j As Integer = 1 To objDataContact.AddrCount()
            Dim addrFields() As String = objDataContact.AddrFetch(j - 1).Split(sPdelim)
            newNode = newNode & "<cntaddr><addrtype>" & addrFields(0) & "</addrtype><address>" & addrFields(1) & "</address><city>" & addrFields(2) & "</city><state>" & addrFields(3) & "</state><postal>" & addrFields(4) & "</postal><country>" & addrFields(5) & "</country></cntaddr>"
        Next
        newNode = newNode & "<cntvoice>" & objDataContact.VoiceTel & "</cntvoice><cnttdd>" & objDataContact.TDDtel & "</cnttdd><cntfax>" & objDataContact.FaxTel & "</cntfax><cntemail>" & objDataContact.Email & "</cntemail><hours>" & objDataContact.Hours & "</hours><cntinst>" & objDataContact.Instructions & "</cntinst></cntinfo>"
        Return newNode
    End Function
End Class
